﻿Imports Microsoft.VisualBasic
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
<Assembly: System.Web.UI.WebResource("ToolKitVB.ToolKit.js", "text/javascript")> 
Namespace Web.UI
    Public Class ToolKitPage
        Inherits System.Web.UI.Page

        ''' <summary>
        ''' Creates an instance of the Javascript.ToolKitTools and makes it available to the ToolKitPage
        ''' </summary>
        Public ReadOnly Property Javascript() As Javascript.ToolKitTools
            Get
                If _Javascript Is Nothing Then _Javascript = New Javascript.ToolKitTools(Me)
                Return _Javascript
            End Get
        End Property
        ''' <summary>
        ''' </summary>
        Public ReadOnly Property ApplicationPath() As String
            Get
                If Request.ApplicationPath = "\" Then
                    Return ""
                Else
                    Return Request.ApplicationPath
                End If
            End Get
        End Property
        Private _Javascript As Javascript.ToolKitTools = Nothing
        ''' <summary>
        ''' RegisterControl for use in Javscript from design view.
        ''' <example>OnPreRender="AddToJavascript"</example>
        ''' </summary>
        Protected Sub AddToJavascript(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim cntrl As Control = CType(sender, Control)
            Javascript.RegisterControl(cntrl)
        End Sub

        Protected Sub RegisterScript(ByVal SrcType As Type, ByVal Src As String)
            Javascript.RegisterScriptSrc(SrcType, Src)
        End Sub

        Protected Sub RegisterCss(ByVal SrcType As Type, ByVal Src As String)
            Javascript.RegisterCssSrc(SrcType, Src)
        End Sub

        Public Sub ClientErrorMessage(ByVal Message As String)
            Javascript.AlertClient(Message)
        End Sub
        Protected Sub ReportError(ByVal ex As Exception, ByVal ClassName As String, ByVal MethodName As String)
            If Server.GetLastError IsNot Nothing Then
                Errors.ToolKitUnexpectedError.Report(Server.GetLastError, ClassName, MethodName)
            End If
        End Sub
        Protected Overrides Sub OnPreLoad(ByVal e As EventArgs)
            MyBase.OnPreLoad(e)
            If _Javascript Is Nothing Then _Javascript = New Javascript.ToolKitTools(Me)
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
            MyBase.OnPreRender(e)
            'Me.Page.Controls.AddAt(Int32.Parse(IIf(Me.Page.Controls.Count > 1, Me.Page.Controls.Count - 1, 1).ToString), New LiteralControl(vbCrLf & "<script type=""text/Javascript"">" & vbCrLf & "try{" & vbCrLf & "onPageLoadComplete();" & vbCrLf & "}catch(ex){ToolKit.errors(ex,'onPageLoadComplete() failed');}" & vbCrLf & "</script>" & vbCrLf))
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim sb As New StringBuilder()
            Dim sw As New StringWriter(sb)
            Dim myHtml As New HtmlTextWriter(sw)
            MyBase.Render(myHtml)
            myHtml.Flush()
            sb = sb.Replace("</body>", Javascript.PageLoadedFunctions() + "</body>")
            writer.Write(sb.ToString())
        End Sub

        Protected Overrides Sub OnError(ByVal e As System.EventArgs)
            Dim LastError As Exception = Server.GetLastError
            Try
                Errors.ToolKitUnexpectedError.Report(LastError, "System.Web.UI.Page OnError", "Unhandled Exception")
            Catch ex As Exception

            End Try
            Throw LastError
            MyBase.OnError(e)
        End Sub

    End Class
End Namespace